/**
 * Created by Dave on 2017/3/16.
 */
var express = require('express');
var http = require('http');
var router = express.Router();
var logger = require('../lib/log4js/log4js').logger('appConfigEnv');
var config = require('../lib/config/config').config;
var jsonUtil = require('../lib/util/jsonUtil');

router.post('/set', function (req, _res, next) {
    var options={
        hostname:config.kubernetes.host,
        port:10087,
        path:"/"+req.username+"/"+req.body.serviceName+"/version="+req.body.version+"/"+req.body.weight,
        method:'POST',
        header:{
            'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
            'Content-Length':0
        }
    }
    var _req=http.request(options, function(req) {
        var html='';
        req.on('data',function(data){
            html+=data;
        });
        req.on('end',function(){
            _res.send(html);
        });
    });
    _req.on('error',function(err){
        console.error(err);
    });
    _req.end();
});
router.post('/get', function (req, _res, next) {

    http.get('http://'+config.kubernetes.host+":10087/"+req.username+"/"+req.body.serviceName+"",function(_req,res){
        var html='';
        _req.on('data',function(data){
            html+=data;
        });
        _req.on('end',function(){
            var data=JSON.parse(html);
            var resp=50;
            for(var name in data){
                console.log(name)
                if(("version="+req.body.version)==name)
                    resp=data[name].weight;
            }
            _res.send(""+resp);
        });
    });
});
module.exports = router;